
import {
  addRepReportParamLabelApi,
  getRepReportParamLabelListApi,
  modifyRepReportParamLabelApi,
  updateDeletedApi
} from '@/services/packetPush.js';
import { message } from 'ant-design-vue';
export default {
  namespaced: true,
  state: {
    addRepReportParamLabelLoading: false,
    repReportParamLabelListLoading: false,
    repReportParamLabelList: [],
    pagination: {
      pageNum: 1,
      pageSize: 10,
      total: 0
    }
  },
  mutations: {
    setState(state, { key, payload }) {
      state[key] = payload;
    }
  },
  actions: {
    // 新增
    async addRepReportParamLabel({ commit }, params) {
      commit('setState', { key: 'addRepReportParamLabelLoading', payload: true });
      try {
        const rep = await addRepReportParamLabelApi(params);
        if (rep.data === '成功') {
          message.success(rep.message);
          return Promise.resolve(true);
        } else {
          message.error(rep.message);
        }
      } catch (error) {
        message.error('操作失败');
      } finally {
        commit('setState', { key: 'addRepReportParamLabelLoading', payload: false });
      }
    },
    // 修改
    async modifyRepReportParamLabel({ commit }, params) {
      commit('setState', { key: 'addRepReportParamLabelLoading', payload: true });
      try {
        const rep = await modifyRepReportParamLabelApi(params);
        if (rep.data === '成功') {
          message.success(rep.message);
          return Promise.resolve(true);
        } else {
          message.error(rep.message);
        }
      } catch (error) {
        message.error('操作失败');
      } finally {
        commit('setState', { key: 'addRepReportParamLabelLoading', payload: false });
      }
    },
    // 删除
    async updateDeleted({ commit }, params) {
      try {
        const rep = await updateDeletedApi(params);
        if (rep.code === 200) {
          message.success(rep.message);
          return Promise.resolve(true);
        } else {
          message.error(rep.message);
        }
      } catch (error) {
        message.error('操作失败');
      }
    },
    // 获取列表
    async getRepReportParamLabelList({ commit, state }, params) {
      commit('setState', { key: 'repReportParamLabelListLoading', payload: true });
      const { pageNum, pageSize } = state.pagination;
      const paramsMerge = {
        pageNum,
        pageSize,
        ...params
      };
      const rep = await getRepReportParamLabelListApi(paramsMerge);
      commit('setState', { key: 'repReportParamLabelList', payload: rep.data.list });
      commit('setState', { key: 'pagination', payload: { ...state.pagination, total: rep.data.total }});
      commit('setState', { key: 'repReportParamLabelListLoading', payload: false });
    },
    // 获取列表  模板管理用
    async getRepReportParamLabelList2({ commit, state }, params) {
      commit('setState', { key: 'repReportParamLabelListLoading', payload: true });
      const paramsMerge = {
        pageNum: 1,
        pageSize: 9999,
        ...params
      };
      const rep = await getRepReportParamLabelListApi(paramsMerge);
      commit('setState', { key: 'repReportParamLabelList', payload: rep.data.list });
      commit('setState', { key: 'pagination', payload: { ...state.pagination, total: rep.data.total }});
      commit('setState', { key: 'repReportParamLabelListLoading', payload: false });
    }
  }
};
